package com.lilicould.controller;

import com.lilicould.service.impl.TenantServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/update/tenant/phones")
public class UpdateTenantPhonesConteoller extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op"); //获取需要进行的操作，删除电话、添加电话或者修改电话信息
        String phone = req.getParameter("phone");
        String tenantId = req.getParameter("tenantId");
        String newPhone = req.getParameter("newPhone");
        System.out.println("op: " + op+" phone: " + phone+" tenantId: " + tenantId);

        TenantServiceImpl tenantService = new TenantServiceImpl(); // 创建租户服务实现类对象
        boolean result = tenantService.updateTenantPhones(op, tenantId, phone,newPhone); // 调用修改租户电话信息方法

        if (result) {
            req.getSession().invalidate();
            req.getSession().setAttribute("success", "已成功修改您的信息");
            System.out.println(req.getContextPath()); // 获取项目路径
            req.getRequestDispatcher("/page/success.jsp").forward(req, resp);
        }else {
            req.getSession().setAttribute("error", "修改失败,请检查输入信息或电话信息是否已存在");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }
    }
}
